package com.zmht.beike.biz.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zmht.beike.biz.TargetManageBiz;
import com.zmht.beike.core.BaseBizImpl;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCaimtostudent;
import com.zmht.beike.po.TCareacontent;
import com.zmht.beike.po.TCmonthcontent;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.beike.po.TCweekcontent;
import com.zmht.beike.po.TCweekinfo;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TIaimtostudent;
import com.zmht.beike.po.TIareacontent;
import com.zmht.beike.po.TIareainfo;
import com.zmht.beike.po.TImonthcontent;
import com.zmht.beike.po.TImonthinfo;
import com.zmht.beike.po.TIweekcontent;
import com.zmht.beike.po.TIweekinfo;
import com.zmht.beike.util.AgeUtil;
import com.zmht.beike.vo.TargetConect;

public class TargetManageBizImpl extends BaseBizImpl implements TargetManageBiz {
	/*
	 * 所有教学目标查询
	 * (non-Javadoc)
	 * @see com.zmht.beike.biz.TargetManageBiz#findAllTarget(com.zmht.beike.vo.TargetConect, int, int)
	 */
	public List findAllTarget(TargetConect content,int startIndex,int pageSize) {
		try{
			if(null==content.getTargetType()){return null;}
			//主题名称 制定时间 制定人 目标类别 目标属性 适用年龄 收藏次数 编 辑 收 藏 
			String sql = "select t.caiminfoName,t.enactmentDate,t.enactmenter,'"+(content.getTargetType()==2?"个别目标":"集体目标")+"','"+(content.getTargetOption()==0?"标准目标":"自定义目标")+"','"+AgeUtil.getAge(content.getAge()+"")+"',t.id,t.concount from t_caiminfo t,teacher tc where 1=1 and t.teacherId=tc.TeacherID";
			String sql2 = "select t.targetContent,t.enactmentDate,t.enactmenter,'"+(content.getTargetType()==2?"个别目标":"集体目标")+"','"+(content.getTargetOption()==0?"标准目标":"自定义目标")+"','"+AgeUtil.getAge(content.getAge()+"")+"',t.id,t.concount from t_iaiminfo t,teacher tc where 1=1 and t.teacherId=tc.TeacherID";
			
		
			if(new Integer(1).equals(content.getTargetType())){
				//目标属性 （标准，自定义)
				sql += " and t.type="+content.getTargetOption();
				if("1".equals(content.getTargetOption().toString())){//如果是自定义的 必须设为共享
					sql += " and t.share=1 ";
					
				}
				if(!"".equals(content.getTeacherName())){
					sql += " and tc.TName like '%"+content.getTeacherName()+"%'";
				}
				//if(!"0".equals(content.getTeacherId())){
				//	sql +=" and t.teacherId="+content.getTeacherId();
				//}
				if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
					sql += " and t.paraValue="+content.getAge();
				}
				if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
					sql += " and t.enactmentDate>='"+content.getStartTime()+"'";
				}
				if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
					sql += " and t.enactmentDate<= '"+content.getEndTime()+"'";
				}
				/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
					sql += " and t.areaId='"+content.getAreaId()+"'";
				}*/
				//如果选择月主题				
				System.out.println(sql);
				return this.getTargetManagerDAO().findBySql(sql, startIndex, pageSize);
			}else{
				//标准 自定义
				sql2 += " and t.type="+content.getTargetOption();
				
				if(!"".equals(content.getTeacherName())){
					sql2 += " and tc.TName like '%"+content.getTeacherName()+"%'";
				}
				
				//if(!"0".equals(content.getTeacherId())){
				//	sql2 +=" and t.teacherId="+content.getTeacherId();
				//}
				if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
					sql2 += " and t.enactmentDate>='"+content.getStartTime()+"'";
				}
				if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
					sql2 += " and t.enactmentDate<= '"+content.getEndTime()+"'";
				}
			
				return this.getTargetManagerDAO().findBySql(sql2, startIndex, pageSize);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public int findAllTargetTotal(TargetConect content) {
		try{
			if(null==content.getTargetType()){return 0;}
			String sql = "select t.caiminfoName,t.enactmentDate,t.enactmenter,'"+(content.getTargetType()==2?"个别目标":"集体目标")+"','"+(content.getTargetOption()==0?"标准目标":"自定义目标")+"','"+AgeUtil.getAge(content.getAge()+"")+"',t.id,t.concount from t_caiminfo t,teacher tc where 1=1 and t.teacherId=tc.TeacherID";
			String sql2 = "select t.targetContent,t.enactmentDate,t.enactmenter,'"+(content.getTargetType()==2?"个别目标":"集体目标")+"','"+(content.getTargetOption()==0?"标准目标":"自定义目标")+"','"+AgeUtil.getAge(content.getAge()+"")+"',t.id,t.concount from t_iaiminfo t,teacher tc where 1=1 and t.teacherId=tc.TeacherID";
			if(new Integer(1).equals(content.getTargetType())){
				//目标属性 （标准，自定义)
				sql += " and t.type="+content.getTargetOption();
				if("1".equals(content.getTargetOption().toString())){//如果是自定义的 必须设为共享
					sql += " and t.share=1 ";
					
				}
				
				if(!"".equals(content.getTeacherName())){
					sql += " and tc.TName like '%"+content.getTeacherName()+"%'";
				}
				//if(!"0".equals(content.getTeacherId())){
				//	sql +=" and t.teacherId="+content.getTeacherId();
				//}
				if(null!=content.getAge()&&!new Integer(0).equals(content.getAge())){
					sql += " and t.paraValue="+content.getAge();
				}
				if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
					sql += " and t.enactmentDate>='"+content.getStartTime()+"'";
				}
				if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
					sql += " and t.enactmentDate<= '"+content.getEndTime()+"'";
				}
				//如果选择月主题				
				/*if(null!=content.getAreaId()&&!new Integer(0).equals(content.getAreaId())){
					sql += " and t.areaId='"+content.getAreaId()+"'";
				}*/		
				return this.getTargetManagerDAO().findAmountBySql(sql);
			}else{
				//标准 自定义
				sql2 += " and t.type="+content.getTargetOption();
				
				
				if(!"".equals(content.getTeacherName())){
					sql2 += " and tc.TName like '%"+content.getTeacherName()+"%'";
				}
				//if(!"0".equals(content.getTeacherId())){
				//	sql2 +=" and t.teacherId="+content.getTeacherId();
			//	}
				if(null!=content.getStartTime()&&!"".equals(content.getStartTime())){
					sql2 += " and t.enactmentDate>='"+content.getStartTime()+"'";
				}
				if(null!=content.getEndTime()&&!"".equals(content.getEndTime())){
					sql2 += " and t.enactmentDate<= '"+content.getEndTime()+"'";
				}
				return this.getTargetManagerDAO().findAmountBySql(sql2);
			}
			
		
		}catch(Exception e){
			e.printStackTrace();
			
		}
		return 0;
		//return this.getTargetManagerDAO().findBySql(sql).size()
	}
		public static void main(String [] args){
		ApplicationContext cxt;
		cxt = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring_dao.xml","spring/spring_beike.xml"});
		TargetManageBizImpl biz = (TargetManageBizImpl)cxt.getBean("targetManagerBizTag");
		TargetConect t = new TargetConect();
		List list = biz.findAllTarget(t,0,10);
		int total = biz.findAllTargetTotal(t);
		System.out.println("   total "+total);
		
	}
		/**
		 * 获得我收藏的目标
		 */
		public List getMyFavorTarget(TargetConect content,int startIndex,int pageSize) {
			return this.getTargetManagerDAO().getMyFavorTarget(content,startIndex,pageSize);
		}
		/**
		 * 获得我收藏的目标个数
		 */
		public int getMyFavorTargetTotal(TargetConect content) {
			
			return this.getTargetManagerDAO().getMyFavorTargetTotal(content);
		}
		/**
		 * 获得我的目标
		 */
		public List getMyTarget(TargetConect content,int startIndex,int pageSize) {
			return this.getTargetManagerDAO().getMyTarget(content,startIndex,pageSize);
		}
		/*
		 * 获得我的目标的个数
		 * (non-Javadoc)
		 * @see com.zmht.beike.biz.TargetManageBiz#getMyTargetTotal(com.zmht.beike.vo.TargetConect)
		 */
		public int getMyTargetTotal(TargetConect content) {
			
			return this.getTargetManagerDAO().getMyTargetTotal(content);
		}
		/*
		 * 保存新目标
		 * (non-Javadoc)
		 * @see com.zmht.beike.biz.TargetManageBiz#saveNewCTarget(java.lang.Integer)
		 */
		public Integer saveNewCTarget(Integer targetId,Integer teacherId) {
			TCaiminfo caiminfo = (TCaiminfo)this.getTargetManagerDAO().findById(TCaiminfo.class, targetId);
			
			TCaiminfo newcaiminfo  = new TCaiminfo();
			Integer id = 0;
			try {
				BeanUtils.copyProperties(newcaiminfo, caiminfo);
				newcaiminfo.setId(null);
				id = (Integer)this.getTargetManagerDAO().saveObj(newcaiminfo);
				
				
				List<TCaimtostudent> list = this.getTargetManagerDAO().findByHQLObj("from TCaimtostudent t where t.caimId = "+targetId);
				for(TCaimtostudent t:list){
					TCaimtostudent tc = new TCaimtostudent();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TCareacontent> tcareas = this.getTargetManagerDAO().findByHQLObj("from TCareacontent t where t.caimId = "+targetId);
				for(TCareacontent t:tcareas){
					TCareacontent tc = new TCareacontent();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TCmonthcontent> tcoms = this.getTargetManagerDAO().findByHQLObj("from TCmonthcontent t where t.caimId = "+targetId);
				for(TCmonthcontent t:tcoms){
					TCmonthcontent tc = new TCmonthcontent();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TCmonthinfo> tcmonths = this.getTargetManagerDAO().findByHQLObj("from TCmonthinfo t where t.caimId = "+targetId);
				for(TCmonthinfo t:tcmonths){
					TCmonthinfo tc = new TCmonthinfo();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				
				List<TCweekcontent> tcweeks = this.getTargetManagerDAO().findByHQLObj("from TCweekcontent t where t.caimId = "+targetId);
				for(TCweekcontent t:tcweeks){
					TCweekcontent tc = new TCweekcontent();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TCweekinfo> tcweekis = this.getTargetManagerDAO().findByHQLObj("from TCweekinfo t where t.caimId = "+targetId);
				for(TCweekinfo t:tcweekis){
					TCweekinfo tc = new TCweekinfo();
					BeanUtils.copyProperties(tc, t);
					tc.setCaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
			} catch (Exception e) {
				
			}
			return id;
		}
		/*
		 * 保存新 个别目标
		 * (non-Javadoc)
		 * @see com.zmht.beike.biz.TargetManageBiz#saveNewITarget(java.lang.Integer)
		 */
		public Integer saveNewITarget(Integer targetId,Integer teacherId) {
			TIaiminfo iaiminfo = (TIaiminfo)this.getTargetManagerDAO().findById(TIaiminfo.class, targetId);
			TIaiminfo newiaiminfo =  new TIaiminfo();
			Integer id = 0;
			try {
				BeanUtils.copyProperties(newiaiminfo, iaiminfo);
				newiaiminfo.setId(null);
				id = (Integer)this.getTargetManagerDAO().saveObj(newiaiminfo);
				
				
				
				List<TIaimtostudent> list = this.getTargetManagerDAO().findByHQLObj("from TIaimtostudent t where t.iaimId = "+targetId);
				for(TIaimtostudent t:list){
					TIaimtostudent tc = new TIaimtostudent();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TIareacontent> iarea = this.getTargetManagerDAO().findByHQLObj("from TIareacontent t where t.iaimId = "+targetId);
				for(TIareacontent t:iarea){
					TIareacontent tc = new TIareacontent();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id+"");
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TIareainfo> tiarea = this.getTargetManagerDAO().findByHQLObj("from TIareainfo t where t.iaimId = "+targetId);
				for(TIareainfo t:tiarea){
					TIareainfo tc = new TIareainfo();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TImonthcontent> timonths = this.getTargetManagerDAO().findByHQLObj("from TImonthcontent t where t.iaimId = "+targetId);
				for(TImonthcontent t:timonths){
					TImonthcontent tc = new TImonthcontent();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				
				List<TImonthinfo> timonthinfos = this.getTargetManagerDAO().findByHQLObj("from TImonthinfo t where t.iaimId = "+targetId);
				for(TImonthinfo t:timonthinfos){
					TImonthinfo tc = new TImonthinfo();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TIweekcontent> tiweekcontents = this.getTargetManagerDAO().findByHQLObj("from TIweekcontent t where t.iaimId = "+targetId);
				for(TIweekcontent t:tiweekcontents){
					TIweekcontent tc = new TIweekcontent();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
				List<TIweekinfo> tiweekinfos = this.getTargetManagerDAO().findByHQLObj("from TIweekinfo t where t.iaimId = "+targetId);
				for(TIweekinfo t:tiweekinfos){
					TIweekinfo tc = new TIweekinfo();
					BeanUtils.copyProperties(tc, t);
					tc.setIaimId(id);
					tc.setId(null);
					this.getTargetManagerDAO().saveObj(tc);
				}
				
			} catch (Exception e) {
				
			}
			
			return id;
		}
		public boolean deleteByHQL(String hql) {
			return this.getTargetManagerDAO().exqHQL(hql);
			
		}
		public List findByHQL(String hql) {
			return this.getTargetManagerDAO().findByHQLObj(hql);
		}
		public List findBySQL(String sql) {
			
			return this.getTargetManagerDAO().findBySql(sql);
		}
	
}
